<template>
	<el-form ref="form" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="username">
			<el-input v-model="form.username" prefix-icon="el-icon-user" clearable
					  :placeholder="$t('login.usernamePlaceholder')"></el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password
					  :placeholder="$t('login.passwordPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item prop="captcha">
			<el-input v-model="form.captcha" prefix-icon="el-icon-warning" clearable
					  :placeholder="$t('login.captchaPlaceholder')">
				<template v-slot:append>
					<el-image
						:src="captchaBase64"
						style="justify-content: center;width: 96px;height: 36px;margin: 0 2px"
						@click="getCaptcha"
					>
						<template #error>
							<span>Loading</span>
						</template>
					</el-image>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px;">
			<el-col :span="12">
				<el-checkbox :label="$t('login.rememberMe')" v-model="form.rememberMe"></el-checkbox>
			</el-col>
			<el-col :span="12" class="login-forgot">
				<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
			</el-col>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="isLogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<div class="login-reg">
			{{ $t('login.noAccount') }}
			<router-link to="/user_register">{{ $t('login.createAccount') }}</router-link>
		</div>
	</el-form>
</template>

<script>
export default {
	data() {
		return {
			isLogin: false,
			captchaBase64: '',
			captchaUUID: "",
			form: {
				username: "",
				password: "",
				captcha: "",
				rememberMe: false
			},
			rules: {
				username: [
					{required: true, message: this.$t('login.usernameError'), trigger: 'blur'}
				],
				password: [
					{required: true, message: this.$t('login.passwordError'), trigger: 'blur'}
				],
				captcha: [
					{required: true, message: this.$t('login.captchaError'), trigger: 'blur'}
				]
			},
		}
	},
	watch: {},
	mounted() {
		this.getCaptcha();
	},
	methods: {
		getCaptcha() {
			this.$API.auth.captcha.get().then(data => {
				this.captchaBase64 = data.captchaBase64;
				this.captchaUUID = data.captchaUUID;
			});
		},
		login() {
			this.$refs.form.validate((valid) => {
				if (!valid) {
					return false
				}

				this.isLogin = true
				const data = {
					username: this.form.username,
					password: this.form.password,
					captcha: this.form.captcha,
					captchaUUID: this.captchaUUID,
					rememberMe: this.form.rememberMe
				};

				// 账号登录
				this.$API.auth.login.post(data).then(loginInfo => {
					this.$TOOL.cookie.set("TOKEN", loginInfo.accessToken, {
						expires: this.form.rememberMe ? loginInfo.expires : 0
					})
					this.$TOOL.data.set("USER_INFO", loginInfo)

					// 获取菜单
					this.$API.manage.permission.oneself.get().then(menu => {
						if (menu.length === 0) {
							this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
								type: 'error',
								center: true
							})
						} else {
							this.$TOOL.data.set("MENU", menu)
							this.$TOOL.data.set("PERMISSIONS", loginInfo.permissions)
						}

						this.$router.replace({
							path: '/'
						})
						this.$message.success("登录成功")

					});

				}).catch(() => {
					this.getCaptcha();
				});
				this.isLogin = false
			});
		},
	}
}
</script>

<style lang="scss" scoped>
:deep(.el-input-group__append, .el-input-group__prepend) {
	padding: 0;
}
</style>
